package com.nowcoder.community.quartz;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.ElasticsearchService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.RedisKeyUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PostScoreRefreshJob implements Job, CommunityConstant {
   private final Logger logger = LoggerFactory.getLogger(PostScoreRefreshJob.class);
   @Autowired
   private RedisTemplate redisTemplate;
   @Autowired
   private DiscussPostService discussPostService;
   @Autowired
   private LikeService likeService;
   @Autowired
   private ElasticsearchService elasticsearchService;
//创建时间 初始时间
   private static final Date epoch;

   static {
       try {
           epoch = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-08-02 00:00:00");
       } catch (ParseException e) {
          throw new RuntimeException("初始化创建时间失败",e);
       }
   }
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
String redisKey = RedisKeyUtil.getPostScoreKey();
        BoundSetOperations operations = redisTemplate.boundSetOps(redisKey);

        if (operations.size() == 0){
            logger.info("任务取消，没有需要刷新的帖子");
            return;
        }

        logger.info("任务开始，正在刷新帖子分数" + operations.size());
while (operations.size()>0){
    this.refresh((Integer) operations.pop());
}
        logger.info("任务结束，帖子分数刷新完毕");

    }

    private void refresh(int postId){
        DiscussPost post = discussPostService.findDiscussPostById(postId);

        if (post == null){
            logger.error("该帖子不存在,postId:"+postId);
            return;
        }

        //是否加精
        boolean wonderful = post.getStatus() == 1;
        //评论数量
        int commentCount = post.getCommentCount();
        //点赞数量
        long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, postId);
        //收藏数量没有设置该功能
        //（精华 + 点赞*2 + 评论*10 + 收藏*2 ）权重
        double w = (wonderful ? 75 : 0) + commentCount * 10 + likeCount * 2;
        // 热帖排行 分数公式 score = log（精华 + 点赞*2 + 评论*10 + 收藏*2 ） + （发布时间 - 初试时间）
        //权重值w有可能小于1 log就会得到负值 调用math.max
double score = Math.log10(Math.max(w,1)) +
        (post.getCreateTime().getTime() - epoch.getTime())/(1000*60*60*24);
//更新帖子分数
        discussPostService.updateScore(postId, score);
        // 同步搜索数据
        post.setScore(score);
        elasticsearchService.saveDiscussPost(post);

    }
}
